package com.xiaoshu.admin.controller;

import java.util.Map;

import javax.servlet.ServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoshu.admin.entity.Employee;
import com.xiaoshu.admin.entity.Students;
import com.xiaoshu.admin.service.EmployeeService;
import com.xiaoshu.common.annotation.SysLog;
import com.xiaoshu.common.base.PageData;
import com.xiaoshu.common.util.ResponseEntity;

@Controller
@RequestMapping("admin/system/emp")
public class EmployeeController {
	
	@Autowired
	private EmployeeService employeeService;
	
	@GetMapping("list")
    @SysLog("跳转员工管理页面")
    public String list(){
        return "admin/emp/list";
    }
	
//	@RequiresPermissions("sys:emp:list")
	@PostMapping("list")
    @ResponseBody
    public PageData<Employee> list(@RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "limit",defaultValue = "10")Integer limit,
            ServletRequest request){
		
		Map map = WebUtils.getParametersStartingWith(request, "s_");
		PageData<Employee> empPageData = new PageData<>();
		QueryWrapper<Employee> empWrapper = new QueryWrapper<>();
		
		if(!map.isEmpty()){
            String sex = (String) map.get("empSex");
            if(StringUtils.isNotBlank(sex)) {
            	empWrapper.eq("empSex", "男".equals(sex) ? "男" : "女");
            }
            String keys = (String) map.get("key");
            if(StringUtils.isNotBlank(keys)) {
            	empWrapper.and(wrapper -> wrapper.like("emp_name", keys).or().like("emp_phone", keys));
            }
        }
		
		IPage<Employee> userPage = employeeService.page(new Page<>(page,limit),empWrapper);
		empPageData.setCount(userPage.getTotal());
		empPageData.setData(userPage.getRecords());
        
		return empPageData;
		
	}
	
	
	@GetMapping("add")
    public String add(ModelMap modelMap){
		/* List<Role> roleList = roleService.selectAll(); */
		/* modelMap.put("roleList",roleList); */
        return "admin/emp/add";
    }
	
	@PostMapping("add")
    @ResponseBody
    public ResponseEntity add(@RequestBody  Employee emp){
		
		if(StringUtils.isBlank(emp.getEmpName())){
            return ResponseEntity.failure("姓名不能为空");
        }
		
		if(StringUtils.isBlank(emp.getEmpSex())) {
			return ResponseEntity.failure("性别不能为空");
		}
		if(StringUtils.isBlank(emp.getEmpPhone())) {
			return ResponseEntity.failure("电话不能为空");
		}
		if(StringUtils.isBlank(emp.getEmpAddress())) {
			return ResponseEntity.failure("地址不能为空");
		}
		employeeService.saveEmp(emp);
		return ResponseEntity.success("操作成功");
	}

}

